package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.quality.domain.req.FinishIntervalPaperReq;
import com.ruibang.glass.quality.entity.FinishIntervalPaper;
import com.ruibang.glass.quality.entity.QualityWarping;
import com.ruibang.glass.quality.entity.QualityWeigh;
import com.ruibang.glass.quality.mapper.FinishIntervalPaperMapper;
import com.ruibang.glass.quality.service.FinishIntervalPaperService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.constant.CommonConstant;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 成品间隔纸受入信息 服务实现类
 * </p>
 *
 * @author wujie
 * @since 2024-03-21
 */
@Service
public class FinishIntervalPaperServiceImpl extends ServiceImpl<FinishIntervalPaperMapper, FinishIntervalPaper> implements FinishIntervalPaperService {

//    @Autowired
//    private RedisSingleService redisService;

    @Autowired
    private RedisSingleService redisService;

    @Override
    public PageResult<FinishIntervalPaper> queryPage(PageRequest<FinishIntervalPaperReq> pageRequest) {
        final FinishIntervalPaperReq param = pageRequest.getParam();
        final LambdaQueryWrapper<FinishIntervalPaper> lambda = new QueryWrapper<FinishIntervalPaper>().lambda();
        this.buildCondition(lambda,param);
        final IPage<FinishIntervalPaper> page = this.page(new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize()), lambda);
        if(page!=null&& CollectionUtils.isNotEmpty(page.getRecords())){
            Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);
            for (FinishIntervalPaper record : page.getRecords()) {
                if(userMap!=null){
                    record.setUserName(userMap.get(record.getUserId())==null?"":userMap.get(record.getUserId()).toString());
                }
            }
        }

        PageResult<FinishIntervalPaper> pr = new PageResult(page);
        return pr;
    }

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(FinishIntervalPaper::getPaperId, id).set(FinishIntervalPaper::getIsDelete, CommonConstant.ONE_STR).update();
    }

    /**
     * 构造查询条件
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<FinishIntervalPaper> lambda, FinishIntervalPaperReq param){
        lambda.eq(FinishIntervalPaper::getIsDelete, CommonConstant.ZERO_STR);
        if(null == param) return;
        if(StringUtils.isNotEmpty(param.getPaperId())){
            lambda.eq(FinishIntervalPaper::getPaperId, param.getPaperId());
        }
        if(StringUtils.isNotEmpty(param.getLotNo())){
            lambda.eq(FinishIntervalPaper::getLotNo, param.getLotNo());
        }
        if(param.getCheckStartTime()!=null&&param.getCheckEndTime()!=null){
            lambda.ge(FinishIntervalPaper::getCheckTime, param.getCheckStartTime());
            lambda.le(FinishIntervalPaper::getCheckTime, param.getCheckEndTime());
        }
        if(StringUtils.isNotEmpty(param.getAbnormal())){
            lambda.like(FinishIntervalPaper::getAbnormal, param.getAbnormal());
        }
        if(StringUtils.isNotEmpty(param.getUseArea())){
            lambda.eq(FinishIntervalPaper::getUseArea, param.getUseArea());
        }
        if(StringUtils.isNotEmpty(param.getGlassModel())){
            lambda.eq(FinishIntervalPaper::getGlassModel, param.getGlassModel());
        }
        if(StringUtils.isNotEmpty(param.getUserId())){
            lambda.eq(FinishIntervalPaper::getUserId, param.getUserId());
        }
        if(StringUtils.isNotEmpty(param.getDecide())){
            lambda.eq(FinishIntervalPaper::getDecide, param.getDecide());
        }
        lambda.orderBy(true,false, FinishIntervalPaper::getCheckTime);
    }
}
